package com.ybear.ybcomponent.widget.linkage

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.constraintlayout.widget.ConstraintLayout
import com.ybear.ybcomponent.R
import com.ybear.ybcomponent.widget.linkage.enums.ViewLinkage
import com.ybear.ybcomponent.Utils

/**
 * @Author MiWi
 * @Date 2025年2月13日
 * @Description Linkage - 联动布局
 */
class LinkageLayoutParams : ConstraintLayout.LayoutParams {
    var viewLinkage: ViewLinkage = ViewLinkage.NONE
    var linkageStartOffset: Float = 0F
    var linkageEndOffset: Float = 0F
    var linkageBindViewId: Int = 0
    var linkageBindView: ViewGroup? = null

    constructor(ctx: Context, attrs: AttributeSet?) : super(ctx, attrs) {
        val attrStyle = ctx.obtainStyledAttributes(
            attrs, R.styleable.LinkageView, 0, 0
        )
        val viewLinkageIndex = attrStyle.getInt(
            R.styleable.LinkageView_linkageScroll, ViewLinkage.NONE.index
        )
        viewLinkage = ViewLinkage.values().find { it.index == viewLinkageIndex } ?: ViewLinkage.NONE
        linkageStartOffset = Utils.px2Dp(
            ctx,
            attrStyle.getDimensionPixelSize(
                R.styleable.LinkageView_linkageStartOffset, 0
            ).toFloat()
        )
        linkageEndOffset = Utils.px2Dp(
            ctx,
            attrStyle.getDimensionPixelSize(
                R.styleable.LinkageView_linkageEndOffset, 0
            ).toFloat()
        )
        linkageBindViewId = attrStyle.getResourceId(
            R.styleable.LinkageView_linkageBindViewId, 0
        )
        attrStyle.recycle()
    }
    constructor(lp: ViewGroup.LayoutParams) : super(lp) {
        fun setMargins(lp: MarginLayoutParams) {
            this.setMargins( lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin )
        }
        when( lp ) {
            is LinkageLayoutParams -> {
                setMargins( lp )
                viewLinkage = lp.viewLinkage
                linkageStartOffset = lp.linkageStartOffset
                linkageEndOffset = lp.linkageEndOffset
                linkageBindViewId = lp.linkageBindViewId
            }
            is MarginLayoutParams -> setMargins( lp )
        }
    }
}